package com.zhc.dejingapi.job;

import com.google.gson.Gson;
import com.zhc.dejingapi.pojo.common.CommonDelMoRoutingDataModel;
import com.zhc.dejingapi.pojo.common.CommonDellOrderTaskModel;
import com.zhc.dejingapi.service.syndb.ICommonDataDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

@Slf4j
@Configuration
public class QingMoOrderBackToLakeJob {

    public  static  boolean isRunning=false;
    private final JdbcTemplate mysqlJdbcTemplate;
    private final ICommonDataDBService commonDataDBService;

    public QingMoOrderBackToLakeJob(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                                    ICommonDataDBService commonDataDBService1) {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
        this.commonDataDBService = commonDataDBService1;
    }

    public  void executeJob() {
        if (isRunning) {
            log.info("QingflowMoOrderUploadJob is running");
            try {
                Thread.sleep(1000 * 60 * 5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        isRunning = true;

        int i = 1;
        if(i==10) {

            //main_Id	appKey	apply_Id	jsondata	adddate	isdeal	errCode	errMsg	nextAction
            String strSql = "select main_Id,appKey,apply_Id,jsondata,adddate,isdeal,errCode,errMsg,nextAction \n" +
                    "from zll_orderbacktolaketask where isdeal=1";

            List<CommonDellOrderTaskModel> list = mysqlJdbcTemplate.query(strSql, new BeanPropertyRowMapper(CommonDellOrderTaskModel.class));

            if (list == null || list.isEmpty()){

            }
            else{
                Gson gson = new Gson();
                for (int j = 0; j < list.size(); j++) {
                    CommonDellOrderTaskModel orderTaskModel = list.get(j);
                    CommonDelMoRoutingDataModel param = gson.fromJson(orderTaskModel.getJsondata(), CommonDelMoRoutingDataModel.class);
                     try{
                         commonDataDBService.DelMoRouting(param, orderTaskModel.getNextAction());
                     }
                     catch (Exception e){
                         e.printStackTrace();
                         log.error(e.getMessage()+"\r\n"+e.getStackTrace());
                     }
                }
            }
        }
        isRunning = false;
    }
}
